import logging
import os
import allure
from utils.data_preprocessor import *


@allure.step("1.解析请求数据")
def analyse_case(case):
    method = case["method"]

    # 判断url是不是mock接口，是mock接口获取mock的基础地址，不是则获取系统基础地址
    # url = os.environ["MOCK_URL"] + case["path"] if case["is_mock"] else os.environ["URL"] + case["path"]
    # url = os.environ["URL"] + case["path"]
    url = "http://192.168.137.129:5173/" + case["path"]

    # 处理请求头、参数、数据等，若对应值是字符串则尝试eval转换，否则设为None
    headers = eval(case["headers"]) if isinstance(case["headers"], str) else None
    params = eval(case["params"]) if isinstance(case["params"], str) else None
    data = eval(case["data"]) if isinstance(case["data"], str) else None


    json = eval(case["json"]) if isinstance(case["json"], str) else None
    files = eval(case["files"]) if isinstance(case["files"], str) else None

    # 组装请求数据字典
    request_data = {
        "method": method,
        "url": url,
        "headers": headers,
        "params": params,
        "data": data,
        "json": json,
        "files": files,
    }

    logging.info(f"1.解析请求数据,请求数据为: {request_data}")
    allure.attach(f"{request_data}", name="解析数据结果")

    return request_data


# if __name__ == "__main__":
#     request_data = analyse_case(case)
#     print(request_data)